package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseMapper;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.entity.House;
import com.atguigu.mapper.CommunityMapper;
import com.atguigu.mapper.DictMapper;
import com.atguigu.mapper.HouseMapper;
import com.atguigu.service.DictService;
import com.atguigu.service.HouseService;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;

/**
 * projectName: shf
 *
 * @author: cbc
 * time: 2022/6/10 19:16 周五
 * description:
 */
@Service(interfaceClass = HouseService.class)
public class HouseServiceImpl extends BaseServiceImpl<House> implements HouseService {
    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private DictService dictService;

    @Override
    protected BaseMapper<House> getEntityMapper() {
        return houseMapper;
    }

    @Override
    public void publish(Long id, Integer status) {
        House house = new House();
        house.setId(id);
        house.setStatus(status);
        houseMapper.update(house);
    }

    @Override
    public PageInfo<HouseVo> findListPage(int pageName, int pageSize, HouseQueryVo houseQueryVo) {
        PageHelper.startPage(pageName, pageSize);
        Page<HouseVo> page = houseMapper.findListPage(houseQueryVo);
        List<HouseVo> list = page.getResult();
        for (HouseVo houseVo : list) {
            //户型
            String houseTypeName = dictMapper.getNameById(houseVo.getHouseTypeId());
            //楼层
            String floorName = dictMapper.getNameById(houseVo.getFloorId());
            //朝向
            String directionName = dictMapper.getNameById(houseVo.getDirectionId());

            houseVo.setHouseTypeName(houseTypeName);
            houseVo.setFloorName(floorName);
            houseVo.setDirectionName(directionName);
        }
        return new PageInfo<HouseVo>(page,10);
    }

////     根据房源id查询
//    @Override
//    public House getById(Serializable id) {
//        House house = houseMapper.getById(id);
//        house.setFloorName(dictMapper.getNameById(house.getFloorId()));
//        house.setHouseTypeName(dictMapper.getNameById(house.getHouseTypeId()));
//        house.setBuildStructureName(dictMapper.getNameById(house.getBuildStructureId()));
//        house.setDirectionName(dictMapper.getNameById(house.getDirectionId()));
//        house.setDecorationName(dictMapper.getNameById(house.getDecorationId()));
//        house.setHouseUseName(dictMapper.getNameById(house.getHouseUseId()));
//        return house;
//    }

    @Override
    public House getById(Serializable id) {
        House house = houseMapper.getById(id);
        if(null == house) return null;

        //户型：
        String houseTypeName = dictService.getNameById(house.getHouseTypeId());
        //楼层
        String floorName = dictService.getNameById(house.getFloorId());
        //建筑结构：
        String buildStructureName = dictService.getNameById(house.getBuildStructureId());
        //朝向：
        String directionName = dictService.getNameById(house.getDirectionId());
        //装修情况：
        String decorationName = dictService.getNameById(house.getDecorationId());
        //房屋用途：
        String houseUseName = dictService.getNameById(house.getHouseUseId());
        house.setHouseTypeName(houseTypeName);
        house.setFloorName(floorName);
        house.setBuildStructureName(buildStructureName);
        house.setDirectionName(directionName);
        house.setDecorationName(decorationName);
        house.setHouseUseName(houseUseName);
        return house;
    }

}
